/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.store.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javaeye.common.util.DateUtils;
import com.javaeye.store.dto.MaterialsChangeDetail;

public class MaterialsChangeDetailDAOHibernate extends HibernateDaoSupport implements MaterialsChangeDetailDAO {

	@Override
	public void save(MaterialsChangeDetail detail) {
		detail.setRecordDate(DateUtils.now());
		getHibernateTemplate().saveOrUpdate(detail);
		//logger.debug("StoreChangeDetail id set to: " + detail.getId());
	}
	
	/**
	 * 取得指定日期期间内的物料的所有批次
	 * @param beginDate
	 * @param materialsId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getBatchNos(Date beginDate, Date endDate, int materialsId) {
		final String queryString = 
			"select distinct m.batchNo from MaterialsChangeDetail m"
			+ " where m.materialsId=? and m.recordDate between ? and ?";
		List result = getHibernateTemplate().find(queryString, new Object[]{materialsId, beginDate, endDate});
		return result;
	}
	
	/**
	 * 取得指定日期期间内的物料ID
	 * @param materialsId
	 * @param monthNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Integer> getMaterialsIds(Date beginDate, Date endDate) {
		final String queryString = 
			"select distinct m.materialsId from MaterialsChangeDetail m " +
			"where m.recordDate between ? and ?";
		List result = getHibernateTemplate().find(queryString, new Object[]{beginDate, endDate});
		return result;
	}
	
	/**
	 * 取得指定日期期间内在库品的库存变动数据汇总
	 * @param beginDate
	 * @param endDate
	 * @param materialsId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<MaterialsChangeDetail> getMaterialsChangeInfo(Date beginDate, Date endDate, int materialsId, String batchNo) {
		final String queryString = 
			"select m.changeType, sum(m.number) as number"
			+ " from MaterialsChangeDetail m"
			+ " where m.materialsId=? and m.batchNo=?"
			+ " and m.recordDate between ? and ?"
			+ " group by m.changeType";
		List result = getHibernateTemplate().find(queryString, new Object[]{materialsId, batchNo, beginDate, endDate});
		List<MaterialsChangeDetail> retValue = new ArrayList<MaterialsChangeDetail>();
		MaterialsChangeDetail d;
		for (Object o : result) {
			Object[] values = (Object[])o;
			d = new MaterialsChangeDetail();
			d.setMaterialsId(materialsId);
			d.setBatchNo(batchNo);
			d.setChangeType((Integer)values[0]);
			d.setNumber(Float.parseFloat(values[1].toString()));
			retValue.add(d);
		}
		return retValue;
	}
	
	/**
	 * 取得指定日期期间内在库品的库存变动数据汇总
	 * @param beginDate
	 * @param endDate
	 * @param materialsId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<MaterialsChangeDetail> getMaterialsChangeInfo(Date beginDate, Date endDate, int materialsId) {
		final String queryString = 
			" from MaterialsChangeDetail m"
			+ " where m.materialsId=?"
			+ " and m.recordDate between ? and ?"
			+ " order by m.recordDate";
		List result = getHibernateTemplate().find(queryString, new Object[]{materialsId, beginDate, endDate});
		return result;
	}

}
